package com.zzvcom.wc;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class WordCountDriver {
    private static final Logger log = LoggerFactory.getLogger(WordCountDriver.class.getName());

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 1 获取job对象
        Job job = Job.getInstance();
        // 2 设置jar包路径 必须
        job.setJarByClass(WordCountDriver.class);
        // 3 关联mapper和reducer
        job.setMapperClass(WordCoutMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setReducerClass(WordCountReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/word/input/wc.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/word/out"));

        boolean result = job.waitForCompletion(true);
        log.info("result:" + result);
    }
    //
    //  在haoop集群上运行
    //  运行前的准备
    // 创建文件，输入一些内容，并上传的hdfs,
    // hdfs dfs -mkdir -p /word/input
    // vi wc.txt
    // hdfs dfs -put wc.txt /word/input

    //使用命令行打包或idea打包
    // mvn clean package
    //然后把打好的jar包上传到服务器上

    // hadoop jar hadoop_pro-1.0-SNAPSHOT.jar com.zzvcom.wc.WordCountDriver
    //重复执行，需要删除hdfs dfs -rm -r -f /word/out，在运行
}
